//~ Project: wslds-blog File: BaseAction.java Created date: Feb 20, 2012
package com.wslds.base.action;

import java.io.File;
import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;


/**
 * @author Domsn
 * 
 */
@SuppressWarnings("serial")
public class BaseAction extends ActionSupport {
	
	public Logger log = Logger.getLogger(this.getClass());

	private static final String CATALOG_BASE_DIR = "upload"; // 目录的根目录
	
	public static final String REDRICT_TO_LIST = "redirect_to_list";
	
	public static final String ERROR_404 = "error404";
	
	public static final String RELOGIN = "relogin";
	
	public String page;
	public String queryString;
	
	/**
	 * 自动跳转到list.action
	 */
	public String index() {
		return REDRICT_TO_LIST;
	}

	public String getAppBaseDir() {
		return getServletContext().getRealPath("/");
	}

	public String getCatalogBaseDir() {
		return getServletContext().getRealPath("/") + File.separator
				+ CATALOG_BASE_DIR;
	}

	public ActionContext getActionContext() {
		return ActionContext.getContext();
	}

	@SuppressWarnings("all")
	public void put(String _param, Object _obj) {
		this.getRequest().put(_param, _obj);
	}

	public Object get(String _param) {
		return this.getRequest().get(_param);
	}

	@SuppressWarnings("all")
	public Map getRequest() {
		return (Map) ActionContext.getContext().get("request");
	}

	public HttpServletRequest getHttpRequest() {
		return ServletActionContext.getRequest();
	}

	@SuppressWarnings("all")
	public Map getSession() {
		return (Map) ActionContext.getContext().getSession();
	}

	public HttpSession getHttpSession() {
		return ServletActionContext.getRequest().getSession();
	}

	public ServletContext getServletContext() {
		return getHttpSession().getServletContext();
	}

	public HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}

	public void writePlainText(String val) {
		this.getResponse().setContentType("text/plain");
		this.getResponse().setCharacterEncoding("UTF-8");
		try {
			this.getResponse().getWriter().write(val);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public int getPage() {
		try {
			return Integer.valueOf(page);
		} catch (Exception e) {
			return 1;
		}
	}

	public void setPage(String page) {
		// System.out.println(page);
		this.page = page;
	}

	public String getQueryString() {
		return queryString;
	}

	public void setQueryString(String queryString) {
		this.queryString = queryString;
	}
}
